package gogogo.liwushuo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.FirstBannerIntentAdapter;
import gogogo.liwushuo.adapter.RecyclerViewAdapter;
import gogogo.liwushuo.bean.FirstBanner2Bean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.listener.OnItemDigestClickListener;
import gogogo.liwushuo.listener.OnItemIconClickListener;
import gogogo.liwushuo.listener.OnItemImageClickListener;
import gogogo.liwushuo.listener.OnItemLikeCountClickListener;
import gogogo.liwushuo.listener.OnItemLikeIconClickListener;
import gogogo.liwushuo.listener.OnItemNameClickListener;
import gogogo.liwushuo.listener.OnItemTitleClickListener;
import gogogo.liwushuo.util.OKHttpUtils;

public class FirstBannerActivity extends AppCompatActivity {

    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;

    private int id;
    private String title;
    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private FirstBannerIntentAdapter adapter;
    private List<FirstBanner2Bean.DataBean.PostsBean> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_banner);
        ButterKnife.inject(this);

        okHttpUtils = OKHttpUtils.newInstance(mContext);
        mContext = this;
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        getJsonData(id);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getJsonData(int id) {
        String intentUrl = Resource.getBanner1IntentUrl(id);
        okHttpUtils.getAsyncMainData(intentUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                FirstBanner2Bean bean =  new Gson().fromJson(result, FirstBanner2Bean.class);
                items = bean.getData().getPosts();
//                Log.i("mtag", "onResponse: " + (bean == null) + "-----" + items.size());
                tvToolbarTitle.setText(bean.getData().getTitle());
                recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
                adapter = new FirstBannerIntentAdapter(items, mContext);
                recyclerview.setAdapter(adapter);

                setListener();
            }
        });
    }

    private void setListener() {
        adapter.setOnItemNameClickListener(new OnItemNameClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                int id = items.get(position).getAuthor().getId();
                Intent intent = new Intent(mContext, HomeAuthorctivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemIconClickListener(new OnItemIconClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv, RecyclerView.ViewHolder holder) {
                int id = items.get(position).getAuthor().getId();
                Intent intent = new Intent(mContext, HomeAuthorctivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemTitltClickListener(new OnItemTitleClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                int id = items.get(position).getColumn().getId();
                Intent intent = new Intent(mContext,HomeTitleActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("title",tv.getText());
                startActivity(intent);
            }
        });
        adapter.setOnItemLikeCountClickListener(new OnItemLikeCountClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv, TextView tv, RecyclerView.ViewHolder holder) {
                FirstBannerIntentAdapter.Holder myHolder = (FirstBannerIntentAdapter.Holder) holder;
                if (myHolder.isLike) {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_normal);
                    tv.setText((Integer.parseInt(tv.getText().toString()) - 1) + "");
                } else {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_selected);
                    tv.setText((Integer.parseInt(tv.getText().toString()) + 1) + "");
                }
                myHolder.isLike = !myHolder.isLike;
            }
        });
        adapter.setOnItemLikeIconClickListener(new OnItemLikeIconClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, ImageView iv,RecyclerView.ViewHolder holder) {
                FirstBannerIntentAdapter.Holder myHolder = (FirstBannerIntentAdapter.Holder) holder;
                if (myHolder.isLike) {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_normal);
                    tv.setText((Integer.parseInt(tv.getText().toString()) - 1) + "");
                } else {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_selected);
                    tv.setText((Integer.parseInt(tv.getText().toString()) + 1) + "");
                }
                myHolder.isLike = !myHolder.isLike;
            }
        });
        adapter.setOnItemImageClickListener(new OnItemImageClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv,RecyclerView.ViewHolder holder) {
                int id = items.get(position).getId();
                Intent intent = new Intent(mContext,HomeImageActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemDigestClickListener(new OnItemDigestClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                int id = items.get(position).getId();
                Intent intent = new Intent(mContext,HomeImageActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
    }
}
